<template>
  <div>
   <div id="app">
    <div>何鑫鑫</div>
    <div class="newadd_button">
      <el-button type="success" size="mini" icon="el-icon-edit" >新增试题</el-button>
    </div>
    <el-form ref="refform" :model="form" label-width="80px" >
      <!-- 布局 -->
      <el-row :gutter="20">
        <el-col :span="6"><div class="grid-content bg-purple">
        <!-- 学科 -->
        <el-form-item label="学科">
         <el-select @change='change' v-model="form.subjectID" placeholder="请选择" >
           <el-option
             v-for="item in subjectoptions"
             :key="item.value"
             :label="item.label"
             :value="item.value">
           </el-option>
          </el-select>
        </el-form-item>
      </div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple">
          <!-- 二级目录 -->
          <el-form-item label="二级目录">
         <el-select v-model="form.catalogID" placeholder="请选择">
           <el-option
             v-for="item in towsubjectoptions"
             :key="item.value"
             :label="item.label"
             :value="item.value">
           </el-option>
          </el-select>
        </el-form-item>
          </div></el-col>
          <!-- 标签 -->
        <el-col :span="6"><div class="grid-content bg-purple">
          <el-form-item label="标签">
         <el-select v-model="form.tags" placeholder="请选择">
           <el-option
             v-for="item in lablelist"
             :key="item.id"
             :label="item.tagName"
             :value="item.id">
           </el-option>
          </el-select>
        </el-form-item>
          </div></el-col>
        <el-col :span="6"><div  class="grid-content bg-purple">
          <el-form-item label="关键字">
          <el-input v-model="form.keyword"></el-input>
        </el-form-item>
          </div></el-col>
      </el-row>
<!-- ///////////////// -->
<!-- ///////////////// -->
<!-- ///////////////// -->
      <el-row :gutter="20">
        <el-col :span="6"><div class="grid-content bg-purple">
        <!-- 学科 -->
        <el-form-item label="试题类形">
         <el-select v-model="form.questionType" placeholder="请选择">
           <el-option
             v-for="item in questiontypeoption"
             :key="item.value"
             :label="item.label"
             :value="item.value">
           </el-option>
          </el-select>
        </el-form-item>
      </div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple">
          <!-- 二级目录 -->
          <el-form-item label="难度">
         <el-select v-model="form.difficulty" placeholder="请选择">
           <el-option
             v-for="item in difficultyoption"
             :key="item.value"
             :label="item.label"
             :value="item.value">
           </el-option>
          </el-select>
        </el-form-item>
          </div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple">
          <el-form-item label="方向">
         <el-select v-model="form.direction" placeholder="请选择">
           <el-option
             v-for="item in fangxiang"
             :key="item"
             :label="item"
             :value="item">
           </el-option>
          </el-select>
        </el-form-item>
          </div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple">
          <el-form-item label="录入人">
           <el-select v-model="form.creatorID" placeholder="请选择">
           <el-option
             v-for="item in enteredoptions"
             :key="item.id"
             :label="item.username"
             :value="item.id">
           </el-option>
          </el-select>
        </el-form-item>
          </div></el-col>
      </el-row>
<!-- ///////////////// -->
<!-- ///////////////// -->
<!-- ///////////////// -->
      <el-row :gutter="20">
        <el-col :span="6"><div class="grid-content bg-purple">
        <!-- 学科 -->
        <el-form-item label="题目备注">
         <el-input v-model="form.remarks"></el-input>
        </el-form-item>
      </div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple">
          <!-- 二级目录 -->
          <el-form-item label="企业简称">
         <el-input v-model="form.shortName"></el-input>
        </el-form-item>
          </div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple">

          <el-form-item label="城市">
            <el-row>
           <el-col :span="12"><div class="grid-content bg-purple">
             <el-select v-model="form.provinces" placeholder="请选择">
           <el-option
             v-for="item in provinces"
             :key="item"
             :label="item"
             :value="item">
           </el-option>
          </el-select></div></el-col>
           <el-col :span="12"><div class="grid-content bg-purple-light">
              <el-select v-model="form.citys" placeholder="请选择">
           <el-option
             v-for="item in gitchengshi"
             :key="item"
             :label="item"
             :value="item">
            </el-option>
            </el-select></div></el-col>
          </el-row>
        </el-form-item>
          </div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple flex">
           <el-button size="mini" class="kuan" @click="resetForm">重置</el-button>
           <el-button size="mini" class="kuan" @click="search"  type="primary">搜索</el-button>
          </div></el-col>
      </el-row>
    </el-form>
<!-- 数据提示 -->
    <el-alert
    :title="'数据一共'+counts+'条'"
    type="info"
    :closable="false"
    show-icon>
  </el-alert>
<!-- 表格 -->
<el-table
      :data="subject"
      style="width: 100%">
      <el-table-column
        prop="number"
        label="试题编号">
      </el-table-column>
      <el-table-column
        prop="subject"
        label="学科">
      </el-table-column>
      <el-table-column
        prop="catalog"
        label="目录">
      </el-table-column>
        <el-table-column
        prop="questionType"
        label="题型">
        <template slot-scope="scope">
          <div>
          {{scope.row.questionType | questiontype}}
          </div>
        </template>
      </el-table-column>
        <el-table-column
        prop="question"
        label="题干">
        <template slot-scope="scope">
          <div v-html="scope.row.question">
          </div>
        </template>
      </el-table-column>
        <el-table-column
        prop="addDate"
        label="录入时间">
        <template slot-scope="scope">
          <div>
          {{scope.row.addDate | sjglq}}
          </div>
        </template>
      </el-table-column>
        <el-table-column
        prop="difficulty"
        label="难度">
        <template slot-scope="scope">
          <div>
          {{scope.row.difficulty | difficultytpye}}
          </div>
        </template>
      </el-table-column>
        <el-table-column
        prop="creator"
        label="录入人">
      </el-table-column>
        <el-table-column
        prop="address"
        label="操作"
        width="180px">
       <template slot-scope="scope">
        <el-button type="primary" icon="el-icon-view" size="mini" @click="see(scope.row)" circle></el-button>
        <el-button type="success" icon="el-icon-edit" size="mini" @click="$router.push('/questions/new?id='+scope.row.id)" circle></el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removet(scope.row)" circle></el-button>
        <el-button type="warning" icon="el-icon-check" size="mini" @click="addselected(scope.row)" circle></el-button>
      </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 10]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="counts">
    </el-pagination>
  </div>
   </div>

   <!-- //查看对话框 -->
   <el-dialog
     title="题目概览"
     :visible.sync="issee"
      @close='close'
     width="90%">
     <el-row :gutter="20">
      <el-col :span="6"><div class="grid-content bg-purple">【题型】：{{preview.questionType | questiontype}}</div></el-col>
      <el-col :span="6"><div class="grid-content bg-purple">【编号】：{{preview.id}}</div></el-col>
      <el-col :span="6"><div class="grid-content bg-purple">【难度】：{{preview.difficulty | difficultytpye}}</div></el-col>
      <el-col :span="6"><div class="grid-content bg-purple">【标签】：{{preview. tags}}</div></el-col>
    </el-row>
    <el-row :gutter="20" style="margin-top:20px">
      <el-col :span="6"><div class="grid-content bg-purple">【学科】：{{preview.subjectName}}</div></el-col>
      <el-col :span="6"><div class="grid-content bg-purple">【目录】：{{preview.directoryName}}</div></el-col>
      <el-col :span="6"><div class="grid-content bg-purple">【方向】：{{preview.direction}}</div></el-col>
    </el-row>
    <hr>
    <el-row><div class="grid-content bg-purple">【题干】：</div></el-row>
    <el-row><div class="grid-content bg-purple" v-html="preview.question"></div></el-row>
    <el-row v-if="preview.questionType !=='3'"><div class="grid-content bg-purple">{{preview.questionType | questiontype}}选项：（以下选中的选项为正确答案）</div></el-row>

    <el-row>
        <el-radio-group v-if="preview.questionType==='1'" v-model="isRight">
          <div v-for="item in preview.options" :key="item.id">
              <el-radio style="margin-top:10px"  disabled  :label="item.isRight">{{item.title}}</el-radio>
          </div>
        </el-radio-group>
          <el-checkbox-group v-if="preview.questionType==='2'" v-model="checked">
          <div v-for="(item) in preview.options" :key="item.id">
              <el-checkbox disabled :label="item.isRight" style="margin-top:3px">{{item.title}}</el-checkbox>
          </div>
         </el-checkbox-group>
    </el-row>
    <hr>
       <el-row><div style="margin:5px 0" class="grid-content bg-purple">
       【参考答案】：<el-button @click="clickspyl" type="danger">视频答案预览</el-button>
        <div class='container'>
          <shipin ref="videoPlayer" :url='videoURL' v-if="spyl"></shipin>
        </div>
       </div></el-row>
     <hr>
     <el-row><div style="margin:5px 0" class="grid-content bg-purple">【答案解析】：{{preview.answer | html2Text}}</div></el-row>
     <hr>
     <el-row><div style="margin:5px 0" class="grid-content bg-purple">【答案备注】：{{preview.remarks}}</div></el-row>
     <span slot="footer" class="dialog-footer">
       <el-button  @click="close">取 消</el-button>
     </span>
   </el-dialog>
  </div>
</template>

<script>
import { list, detail, remove, choiceAdd } from '@/api/hmmm/questions'
import { simple } from '@/api/hmmm/subjects'
import { simple as towsimple } from '@/api/hmmm/directorys'
import { list as labellist } from '@/api/hmmm/tags'
import { simple as listentered } from '@/api/base/users'
import { provinces, citys } from '@/api/hmmm/citys'
import shipin from '@/components/video'
export default {
  data () {
    return {
      videoURL: '',
      spyl: false,
      checked: [1],
      issee: false,
      subjectoptions: [],
      // 二级目录
      towsubjectoptions: [],
      // 标签
      lablelist: [],
      subject: [],
      enteredoptions: [],
      questiontypeoption: [{
        value: '1',
        label: '单选'
      }, {
        value: '2',
        label: '多选'
      }, {
        value: '3',
        label: '简答'
      }],
      difficultyoption: [{
        value: '1',
        label: '简单'
      }, {
        value: '2',
        label: '普通'
      }, {
        value: '3',
        label: '困难'
      }],
      fangxiang: [
        'o2o',
        '外包服务',
        '企业服务',
        '互联网金融',
        '企业咨询',
        '互联网',
        '电子商务',
        '其他'
      ],
      preview: {
        questionType: '',
        id: '',
        difficulty: '',
        tags: '',
        subject: '',
        catalog: '',
        direction: '',
        question: '',
        remarks: '',
        answer: ''
      },
      form: {
        subjectID: '',
        catalogID: '',
        tags: '',
        keyword: '',
        questionType: '',
        difficulty: '',
        direction: '',
        creatorID: '',
        remarks: '',
        shortName: '',
        provinces: '',
        citys: ''
      },
      isRight: 1,
      page: 1,
      pagesize: 5,
      counts: 0
    }
  },

  components: {
    shipin
  },
  computed: {
    provinces,
    gitchengshi () {
      return citys(this.form.provinces)
    }
  },
  created () {
    this.getentered()
    this.gitsimplelist2()
    this.getlist()
  },

  methods: {
    close () {
      this.issee = false
      this.spyl = false
    },
    clickspyl () {
      this.spyl = true
    },
    async addselected (row) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await choiceAdd({ id: row.id, choiceState: 1 })
        this.getlist()
        this.$message({
          type: 'success',
          message: '加入成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消加入'
        })
      })
    },
    // 删除
    removet (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.getlist()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 查看
    async see (row) {
      const res = await detail(row)
      this.videoURL = res.data.videoURL
      this.preview = res.data
      this.issee = true
    },
    // 重置
    resetForm () {
      this.form = {
        subjectID: '',
        catalogID: '',
        tags: '',
        keyword: '',
        questionType: '',
        difficulty: '',
        direction: '',
        creatorID: '',
        remarks: '',
        shortName: '',
        provinces: '',
        citys: ''
      }
    },
    change () {
      this.form.catalogID = []
      this.gittowlist()
    },
    async search () {
      const res = await list(this.form)
      console.log(res)
      this.subject = res.data.items
    },
    // 获取简单题库列表
    async gitsimplelist2 () {
      const res = await simple()
      this.subjectoptions = res.data
    },
    // 获取二级目录
    async gittowlist () {
      const res = await towsimple({ subjectID: this.form.subjectID })
      const res1 = await labellist({ subjectID: this.form.subjectID })
      this.towsubjectoptions = res.data
      this.lablelist = res1.data.items
    },
    // 获取录入人
    async getentered () {
      const res2 = await listentered()
      this.enteredoptions = res2.data
    },
    // 获取题库列表
    async getlist () {
      const res = await list({
        page: this.page,
        pagesize: this.pagesize
      })
      this.counts = res.data.counts
      this.subject = res.data.items
    },
    // 分页控制
    handleSizeChange (val) {
      this.pagesize = val
      this.getlist()
    },
    handleCurrentChange (val) {
      this.page = val
      this.getlist()
    }
  }
}
</script>

<style lang='scss' scoped>
#app{
  background-color: #fff;
  margin: 5px;
  padding: 20px;
  border-radius: 5px;
}
.newadd_button{
  display: flex;
  margin: 10px 0;
  justify-content:flex-end
}
.el-pagination{
   margin-top: 10px;
  display: flex;
  justify-content: flex-end;
}
.flex{
display: flex;
justify-content: flex-end;
}
.kuan{
  height: 36px;
}
.container{
  width: 500px;
}
</style>
